package cs.xwi7xwa.data;

import java.io.IOException;

import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public abstract class DatabaseAdapter
{
    protected DatabaseHelper dbHelper;
    protected SQLiteDatabase db;
    protected final Context myContext;

    public DatabaseAdapter(Context context)
    {
        this.myContext = context;
    }
    
    /**
     * Loads the pregenerated database from /assets/. If it cannot be opened,
     * try to create a new instance of the database. If it cannot be created,
     * throw an exception to signal the failure.
     * 
     * @return this (self reference, allowing this to be chained in an
     *         initialization call)
     * @throws IOException
     *             if the loading has failed
     * @throws SQLException
     *             if the database could be neither opened or created
     */
    public DatabaseAdapter open() throws IOException
    {
        dbHelper = new DatabaseHelper(myContext);
        dbHelper.createDataBase();
        db = dbHelper.openDataBase();
        return this;
    }

    /**
     * Closes the database
     */
    public void close()
    {
        dbHelper.close();
    }
}
